package com.exercise.AndroidCamera;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import com.exercise.AndroidCamera.AddFriendActivity.sendFriendInfoAsync;
import com.exercise.AndroidCamera.ModifyInfoActivity.sendUserInfoAsync;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
/*
기능 : 서버와 연동하여 친구 정보받기
작성 : 하드캔
완료일 : 2014.09.26 
	*/
public class FriendInfoActivity extends BaseActivity {

	Intent mIntent;
	String userID;
	String friendID;
	
	String name = "";
	String phone = "";
	String email = "";
	
	EditText editID;
	EditText editName;
	EditText editPhone;
	EditText editEmail;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friend_info);
		
		userID = getIntent().getExtras().getString("userID");
		friendID = getIntent().getExtras().getString("friendID");
		
		editID = (EditText)this.findViewById(R.id.editID1);
		editName = (EditText)this.findViewById(R.id.editName1);
		editPhone = (EditText)this.findViewById(R.id.editPhone1);
		editEmail = (EditText)this.findViewById(R.id.editEmail1);

		getActionBar().setTitle("Post Memories");
		getActionBar().setDisplayShowHomeEnabled(false);
		
		findViewById(R.id.friend_info_Ok).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						onBackPressed();
					}
				});
		
		readUserInfoAsync myTask = new readUserInfoAsync();
		myTask.execute();					
	}
	
	protected String[] load_user_info(Context context){
		String pString = null;
		HttpURLConnection conn = null;
		BufferedReader br = null;
		
		String[] result = new String[2];
		result[0] = "error"; //JSON flag
		result[1] = null; //message
		
		try{
       	StringBuilder builder = new StringBuilder();
       	
       	URL url = new URL("http://54.199.194.197/get_user_info.php");
       	conn = (HttpURLConnection)url.openConnection();
       	
       	if(conn != null){
       		conn.setConnectTimeout(15000);	
       		conn.setReadTimeout(15000);
       		conn.setDefaultUseCaches(false); 
           	conn.setDoInput(true); 
           	conn.setDoOutput(true);  
           	conn.setRequestMethod("POST");
           	conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
              
         	StringBuffer buffer = new StringBuffer();
         	
         	buffer.append("user_id").append("=").append(friendID);
         	
            
            OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            PrintWriter writer = new PrintWriter(outStream);
            writer.write(buffer.toString());
            writer.flush();
            
           InputStreamReader inputStream = new InputStreamReader(conn.getInputStream(), "UTF-8"); 
           br = new BufferedReader(inputStream);
              
           	String line;
           	while((line = br.readLine()) != null){
           	   builder.append(line);
           	}
              
           	br.close();

           	pString =  builder.toString();
           	Log.d("pString", pString);
           	
           	JSONObject jObject = new JSONObject(pString);
           	JSONArray nameObject = new JSONArray(jObject.getString("name"));
           	JSONArray phoneObject = new JSONArray(jObject.getString("phone"));
           	JSONArray emailObject = new JSONArray(jObject.getString("email"));
           	
           	name = nameObject.getString(0);
           	phone = phoneObject.getString(0);
           	email = emailObject.getString(0);           	
       	}else{
       		
       		throw new Exception("network error");
       	}
       	
       }catch(SocketTimeoutException se){
       	
       	result[1] = "network error";
       	
       }catch(Exception e){
       	
       	result[1] = e.getMessage();
       	
       }finally{
       	
       	if(conn != null)
       		conn.disconnect();

       	if(br != null){
       		try {
       			br.close();
				} catch (Exception e2) {
					// TODO: handle exception
					result[1] = e2.getMessage();
				}
       	}
       }
       return result;
	}
	
	public class readUserInfoAsync extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {

			super.onPreExecute();
		}
		@Override
		protected String doInBackground(String... params) {
			load_user_info(FriendInfoActivity.this);
			return null;
		}
		@Override
		protected void onPostExecute(String result) {
			editID.setText(friendID);
			editName.setText(name);
			editPhone.setText(phone);
			editEmail.setText(email);
			
			super.onPostExecute(result);
		}
		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}
}
